export interface IpInfo {
    ip: string;
    country?: string;
    country_code?: string;
    region?: string;
    region_code?: string;
    city?: string;
    isp?: string;
    org?: string;
    asn?: string;
    network?: string;
    version?: string;
    latitude?: number;
    longitude?: number;
    timezone?: string;
    utc_offset?: string;
    postal?: string;
    currency?: string;
    currency_name?: string;
    country_capital?: string;
    country_tld?: string;
    continent_code?: string;
    country_calling_code?: string;
    languages?: string;
    country_area?: number;
    country_population?: number;
    accuracy?: string;
}

/**
 * 获取当前访问者的公网IP地址
 */
export async function getPublicIp(): Promise<IpInfo | null> {
    try {
        const response = await fetch('https://ipapi.co/json/');

        if (!response.ok) {
            console.warn('ipapi.co response not ok:', response.status, response.statusText);
            return null;
        }

        const data = await response.json();

        return {
            ip: data.ip,
            country: data.country_name,
            country_code: data.country_code,
            region: data.region,
            region_code: data.region_code,
            city: data.city,
            isp: data.org,
            org: data.org,
            asn: data.asn,
            network: data.network,
            version: data.version,
            latitude: data.latitude,
            longitude: data.longitude,
            timezone: data.timezone,
            utc_offset: data.utc_offset,
            postal: data.postal,
            currency: data.currency,
            currency_name: data.currency_name,
            country_capital: data.country_capital,
            country_tld: data.country_tld,
            continent_code: data.continent_code,
            country_calling_code: data.country_calling_code,
            languages: data.languages,
            country_area: data.country_area,
            country_population: data.country_population,
            accuracy: '基于ipapi.co判断'
        };
    } catch (error) {
        console.warn('Failed to fetch IP info:', error);
        return null;
    }
}

/**
 * 查询指定IP地址的详细信息
 */
export async function getIpInfo(ip: string): Promise<IpInfo | null> {
    try {
        const response = await fetch(`https://ipapi.co/${ip}/json/`);

        if (!response.ok) {
            console.warn('ipapi.co response not ok:', response.status, response.statusText);
            return null;
        }

        const data = await response.json();

        return {
            ip,
            country: data.country_name,
            country_code: data.country_code,
            region: data.region,
            region_code: data.region_code,
            city: data.city,
            isp: data.org,
            org: data.org,
            asn: data.asn,
            network: data.network,
            version: data.version,
            latitude: data.latitude,
            longitude: data.longitude,
            timezone: data.timezone,
            utc_offset: data.utc_offset,
            postal: data.postal,
            currency: data.currency,
            currency_name: data.currency_name,
            country_capital: data.country_capital,
            country_tld: data.country_tld,
            continent_code: data.continent_code,
            country_calling_code: data.country_calling_code,
            languages: data.languages,
            country_area: data.country_area,
            country_population: data.country_population,
            accuracy: '基于ipapi.co判断'
        };
    } catch (error) {
        console.warn('Failed to fetch IP info:', error);
        return null;
    }
} 